package com.xtianzhuang.www.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xtianzhuang.www.dao.TdCatalogMapper;
import com.xtianzhuang.www.dao.TdCityMapper;
import com.xtianzhuang.www.dao.TdProvincialMapper;
import com.xtianzhuang.www.dao.TdSStaticMapper;
import com.xtianzhuang.www.dao.VRolePermissionMapper;
import com.xtianzhuang.www.dao.VUserAccountMapper;
import com.xtianzhuang.www.dao.VUserPermissionMapper;
import com.xtianzhuang.www.dao.VUserStoryMapper;
import com.xtianzhuang.www.entity.TdCatalog;
import com.xtianzhuang.www.entity.TdCatalogExample;
import com.xtianzhuang.www.entity.TdCity;
import com.xtianzhuang.www.entity.TdCityExample;
import com.xtianzhuang.www.entity.TdProvincial;
import com.xtianzhuang.www.entity.TdProvincialExample;
import com.xtianzhuang.www.entity.TdSStatic;
import com.xtianzhuang.www.entity.TdSStaticExample;
import com.xtianzhuang.www.entity.VUserAccount;
import com.xtianzhuang.www.entity.VUserAccountExample;
import com.xtianzhuang.www.entity.VUserPermission;
import com.xtianzhuang.www.entity.VUserPermissionExample;
import com.xtianzhuang.www.entity.VUserStory;
import com.xtianzhuang.www.entity.VUserStoryExample;
import com.xtianzhuang.www.entity.constants.IsDeletedConstants;
import com.xtianzhuang.www.entity.constants.PayTypeConstants;
import com.xtianzhuang.www.entity.request.AddDataIdRequest;
import com.xtianzhuang.www.entity.request.AddTypeIdRequest;
import com.xtianzhuang.www.entity.request.DeleteSystemStaticRequest;
import com.xtianzhuang.www.service.SystemService;
import com.xtianzhuang.www.service.base.BaseServiceBiz;

@Service("systemServiceBiz")
@Transactional(rollbackFor = Exception.class)
public class SystemServiceBiz extends BaseServiceBiz implements SystemService {
	@Autowired
	protected TdSStaticMapper tdSStaticMapper;
	@Autowired
	protected TdProvincialMapper tdProvincialMapper;
	@Autowired
	protected TdCityMapper tdCityMapper;
	@Autowired
	protected VUserAccountMapper vUserAccountMapper;
	@Autowired
	protected TdCatalogMapper tdCatalogMapper;
	@Autowired
	protected VRolePermissionMapper vRolePermissionMapper;
	@Autowired
	protected VUserPermissionMapper vUserPermissionMapper;
	@Autowired
	protected VUserStoryMapper vUserStoryMapper;
	@Override
	public List<TdSStatic> TypeIdQuery() {
		// TODO Auto-generated method stub
		TdSStaticExample example = new TdSStaticExample();
		example.or().andTypeIdEqualTo("TYPE_ID").andIsDeletedEqualTo(IsDeletedConstants.IN_USE);
		return tdSStaticMapper.selectByExample(example);
	}

	@Override
	public List<TdProvincial> selectProvince() {
		// TODO Auto-generated method stub
		TdProvincialExample example = new TdProvincialExample();
		return tdProvincialMapper.selectByExample(example);
	}

	@Override
	public List<TdCity> selectCity(String provinceCode) {
		// TODO Auto-generated method stub
		TdCityExample example = new TdCityExample();
		example.or().andPidEqualTo(Integer.valueOf(provinceCode));
		return tdCityMapper.selectByExample(example);
	}

	@Override
	public List<TdCity> selectCity() {
		TdCityExample example = new TdCityExample();
		return tdCityMapper.selectByExample(example);
	}

	@Override
	public List<VUserAccount> selectAccount() {
		// TODO Auto-generated method stub
		VUserAccountExample vUserAccountExample = new VUserAccountExample();
		vUserAccountExample.or().andUserIdEqualTo(subject.getPrincipal().getUserId());
		return vUserAccountMapper.selectByExample(vUserAccountExample);
	}

	@Override
	public List<TdCatalog> selectCatalog1(String type) {
		TdCatalogExample example = new TdCatalogExample();
		// TODO Auto-generated method stub
		if (StringUtils.isNotEmpty(type)) {
			if (type.equals(PayTypeConstants.PAY)) {
				example.or().andCatalogTypeEqualTo("pay").andIsDeletedEqualTo(IsDeletedConstants.IN_USE)
						.andParantCatalogIdIsNotNull();
				return tdCatalogMapper.selectByExample(example);
			} else if (type.equals(PayTypeConstants.INCOME)) {
				example.or().andCatalogTypeEqualTo("income")
						.andIsDeletedEqualTo(IsDeletedConstants.IN_USE);
				return tdCatalogMapper.selectByExample(example);
			}
		}

		return new ArrayList<TdCatalog>();
	}

	@Override
	public List<TdCatalog> selectCatalog2(String parentCatalogId) {
		TdCatalogExample example = new TdCatalogExample();
		// TODO Auto-generated method stub
		if (StringUtils.isNotEmpty(parentCatalogId)) {
			example.or().andParantCatalogIdEqualTo(Integer.valueOf(parentCatalogId)).andIsDeletedEqualTo("0");
			return tdCatalogMapper.selectByExample(example);
		}
		return new ArrayList<TdCatalog>();
	}

	@Override
	public List<TdCatalog> selectCatalog3() {
		// TODO Auto-generated method stub
		TdCatalogExample example = new TdCatalogExample();
		example.or().andCatalogTypeEqualTo("pay").andIsDeletedEqualTo(IsDeletedConstants.IN_USE)
				.andParantCatalogIdIsNotNull();
		example.or().andCatalogTypeEqualTo("income").andIsDeletedEqualTo(IsDeletedConstants.IN_USE);
		return tdCatalogMapper.selectByExample(example);
	}

	@Override
	public List<TdSStatic> queryStaticByTypeId(String typeId) {
		TdSStaticExample example = new TdSStaticExample();
		example.or().andTypeIdEqualTo(typeId).andIsDeletedEqualTo(IsDeletedConstants.IN_USE);
		return tdSStaticMapper.selectByExample(example);
	}

	@Override
	public long queryStaticCountByTypeId(String typeId) {
		TdSStaticExample example = new TdSStaticExample();
		example.or().andTypeIdEqualTo(typeId).andIsDeletedEqualTo(IsDeletedConstants.IN_USE);
		return tdSStaticMapper.countByExample(example);
	}

	@Override
	public void deleteSystemStatic(DeleteSystemStaticRequest request) {
		// TODO Auto-generated method stub
		TdSStatic tdSStatic = new TdSStatic();
		tdSStatic.setIsDeleted(IsDeletedConstants.DELETED);
		tdSStatic.setId(request.getId());
		tdSStaticMapper.updateByPrimaryKeySelective(tdSStatic);
	}

	@Override
	public void addTypeId(AddTypeIdRequest request) {
		// TODO Auto-generated method stub
		TdSStatic tdSStatic = new TdSStatic();
		tdSStatic.setTypeId("TYPE_ID");
		tdSStatic.setCode(request.getCode());
		tdSStatic.setIsDeleted(IsDeletedConstants.IN_USE);
		tdSStatic.setCreateStaffId(subject.getPrincipal().getUserName());
		tdSStatic.setDataId(request.getDataId());
		tdSStatic.setDataName(request.getDataName());
		tdSStatic.setCreateTime(new Date());
		tdSStaticMapper.insert(tdSStatic);
	}

	@Override
	public void addDataId(AddDataIdRequest request) {
		// TODO Auto-generated method stub
		TdSStatic tdSStatic = new TdSStatic();
		tdSStatic.setTypeId(request.getTypeId());
		tdSStatic.setCode(request.getCode());
		tdSStatic.setIsDeleted(IsDeletedConstants.IN_USE);
		tdSStatic.setCreateStaffId(subject.getPrincipal().getUserName());
		tdSStatic.setDataId(request.getDataId());
		tdSStatic.setDataName(request.getDataName());
		tdSStatic.setCreateTime(new Date());
		tdSStaticMapper.insert(tdSStatic);
	}

	@Override
	public List<VUserPermission> getPermissions(Integer userId) {
		// TODO Auto-generated method stub
		VUserPermissionExample example = new VUserPermissionExample();
		example.or().andUserIdEqualTo(userId);
		return vUserPermissionMapper.selectByExample(example);
	}

	@Override
	public List<VUserStory> selectUserBudgetStory() {
		// TODO Auto-generated method stub
		VUserStoryExample example = new VUserStoryExample();
		example.or().andUserIdEqualTo(subject.getPrincipal().getUserId()).andIsDeletedEqualTo(IsDeletedConstants.IN_USE)
				.andStoryTypeEqualTo("budget");
		return vUserStoryMapper.selectByExampleWithBLOBs(example);
	}

}
